package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.retrofitModule.response.GetPrizeHistroyResponse;
import com.nex3z.flowlayout.FlowLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.AreaCircle;
import utils.StringUtils;
import utils.Utils;

/**
 * Created by ASUS on 2017/9/18.
 */

public class PrizeListAdapter extends BaseAdapter {
    private ArrayList<GetPrizeHistroyResponse> getPrizeHistroyResponses;
    private Context mContext;
    private String code;
    private boolean mIsPrizeTime;

    public PrizeListAdapter(ArrayList<GetPrizeHistroyResponse> getPrizeHistroyResponses, Context context, String code, boolean mIsPrizeTime) {
        this.getPrizeHistroyResponses = getPrizeHistroyResponses;
        mContext = context;
        this.code = code;//安徽快三是特殊情况
        this.mIsPrizeTime = mIsPrizeTime;
    }

    public void setData(ArrayList<GetPrizeHistroyResponse> getPrizeHistroyResponses, String code, boolean mIsPrizeTime) {
        this.getPrizeHistroyResponses = getPrizeHistroyResponses;
        this.code = code;//安徽快三是特殊情况
        this.mIsPrizeTime = mIsPrizeTime;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        GetPrizeHistroyResponse prizeBean = getPrizeHistroyResponses.get(position);
        if ("ahk3".equals(code) || "jiangsuk3".equals(code) || Common.isK3(code)) {
            ViewHolder1 holder;
            if (view != null) {
                holder = (ViewHolder1) view.getTag();
            } else {
                view = LayoutInflater.from(mContext).inflate(R.layout.layout_history_item1, null);
                holder = new ViewHolder1(view);
                view.setTag(holder);
            }

//                holder.name.setText(getPrizeHistroyResponses.get(position).getCreated_at().split(" ")[1] + " " + getPrizeHistroyResponses.get(position).getIssue_no());
            if (position % 2 == 0) {
                holder.itemContainer.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            } else {
                holder.itemContainer.setBackgroundColor(mContext.getResources().getColor(R.color.grey_bg));
            }
            holder.name.setText(prizeBean.getIssue_no());
            holder.time_txt.setText(prizeBean.getTime());
            holder.container.removeAllViews();
            String[] p = prizeBean.getPrize_num().split(",");
            String prizeString = prizeBean.getPrize_num().replaceAll(",", " ");
            int sum = 0;
            for (String s : p) {
                ImageView dice = new ImageView(mContext);
                dice.setImageResource(Common.getDiceDrawable(StringUtils.parseInt(s)));
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 18), Utils.dip2px(mContext, 18));
                layoutParams.rightMargin = Utils.dip2px(mContext, 6);
                dice.setLayoutParams(layoutParams);
                holder.container.addView(dice);

                if (Utils.isNumeric(s) && !s.equals("")) {
                    sum += Integer.parseInt(s);
                }
            }

            if (mIsPrizeTime && position == 0) {
                holder.container.setVisibility(View.GONE);
                holder.prizeNum.setVisibility(View.GONE);
                holder.descp.setVisibility(View.GONE);
                holder.prize_time.setVisibility(View.VISIBLE);
            } else {
                holder.container.setVisibility(View.VISIBLE);
                holder.prizeNum.setVisibility(View.VISIBLE);
                holder.descp.setVisibility(View.VISIBLE);
                holder.prize_time.setVisibility(View.GONE);
            }

            holder.prizeNum.setText(prizeString);
            holder.descp.setText(sum + "   " + (sum <= 10 && sum >= 4 ? "小" : (sum <= 17 && sum >= 11 ? "大" : "通吃")) + "   " + (sum % 2 == 1 ? "单" : "双"));
        } else if ("xglhc".equals(code) || "jslhc".equals(code) || "js10flhc".equals(code)) {
            ViewHolder2 holder;
            if (view != null) {
                holder = (ViewHolder2) view.getTag();
            } else {
                view = LayoutInflater.from(mContext).inflate(R.layout.layout_history_item2, null);
                holder = new ViewHolder2(view);
                view.setTag(holder);
            }
            if (position % 2 == 0) {
                holder.llContent.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            } else {
                holder.llContent.setBackgroundColor(mContext.getResources().getColor(R.color.grey_bg));
            }
            holder.name.setText(prizeBean.getIssue_no());
            holder.container.removeAllViews();
            String[] p = prizeBean.getPrize_num().split(",");

            for (int i = 0; i < p.length; i++) {
                TextView areaCircle = new TextView(mContext);
                areaCircle.setText(p[i]);
                areaCircle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                areaCircle.setGravity(Gravity.CENTER);
                areaCircle.setTextColor(Color.parseColor("#3f3f3f"));
                LinearLayout linearLayout = new LinearLayout(mContext);
                linearLayout.setOrientation(LinearLayout.VERTICAL);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 25), Utils.dip2px(mContext, 25));
                layoutParams.rightMargin = Utils.dip2px(mContext, 3);
                areaCircle.setLayoutParams(layoutParams);
                linearLayout.addView(areaCircle);
                TextView textView = new TextView(mContext);
                if (getPrizeHistroyResponses.size() > 0 && prizeBean.getAttached() != null && prizeBean.getAttached().getSx() != null && prizeBean.getAttached().getSx().size() > 0) {
                    switch (prizeBean.getAttached().getColor().get(i)) {
                        case "blue":
                            areaCircle.setBackgroundResource(R.mipmap.blue_big);
                            break;
                        case "red":
                            areaCircle.setBackgroundResource(R.mipmap.red_big);
                            break;
                        case "green":
                            areaCircle.setBackgroundResource(R.mipmap.green_big);
                            break;
                    }
                    textView.setText(prizeBean.getAttached().getSx().get(i));
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                    textView.setTextColor(Color.parseColor("#3f3f3f"));
                    LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    layoutParams1.gravity = Gravity.CENTER_HORIZONTAL;
                    textView.setLayoutParams(layoutParams1);
                }

                linearLayout.addView(textView);
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                linearLayout.setLayoutParams(layoutParams2);
                layoutParams2.gravity = Gravity.CENTER_HORIZONTAL;
                holder.container.addView(linearLayout);

                if (i == p.length - 2) {//倒数第二个
                    TextView add = new TextView(mContext);
                    add.setTextColor(Color.parseColor("#dfdfdf"));
                    add.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
                    add.setText("+");
                    LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    layoutParams1.rightMargin = Utils.dip2px(mContext, 3);
                    layoutParams1.weight = 1.0f;
                    layoutParams1.gravity = Gravity.TOP;
                    add.setLayoutParams(layoutParams1);
                    holder.container.addView(add);
                }
            }

            if (mIsPrizeTime && position == 0) {
                holder.container.setVisibility(View.GONE);
                holder.prize_time.setVisibility(View.VISIBLE);
            } else {
                holder.container.setVisibility(View.VISIBLE);
                holder.prize_time.setVisibility(View.GONE);
            }
        } else {
            ViewHolder holder;
            if (view != null) {
                holder = (ViewHolder) view.getTag();
            } else {
                view = LayoutInflater.from(mContext).inflate(R.layout.layout_history_item, null);
                holder = new ViewHolder(view);
                view.setTag(holder);
            }

//                holder.name.setText(getPrizeHistroyResponses.get(position).getCreated_at().split(" ")[1] + " " + getPrizeHistroyResponses.get(position).getIssue_no());
            if (position % 2 == 0) {
                holder.itemContainerBg.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            } else {
                holder.itemContainerBg.setBackgroundColor(mContext.getResources().getColor(R.color.grey_bg));
            }
            holder.name.setText(prizeBean.getIssue_no());
            holder.time_txt.setText(prizeBean.getTime());
            holder.container.removeAllViews();
            String[] p = prizeBean.getPrize_num().split(",");
            // 这里需要5个小球换一行
            int index = 0;
            LinearLayout.LayoutParams lineParams =  new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            LinearLayout line = null;
            for (String s : p) {
                if (Common.isK3(code)) {
                    ImageView dice = new ImageView(mContext);
                    dice.setImageResource(Common.getDiceDrawable(StringUtils.parseInt(s)));
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 20), Utils.dip2px(mContext, 20));
                    layoutParams.rightMargin = Utils.dip2px(mContext, 11);
                    dice.setLayoutParams(layoutParams);
                    holder.container.addView(dice);
                } else {
                    AreaCircle areaCircle = new AreaCircle(mContext);
                    Common.setBallBackground(areaCircle, s, code);
                    areaCircle.setText(s);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 20), Utils.dip2px(mContext, 20));
                    layoutParams.rightMargin = Utils.dip2px(mContext, 11);
                    areaCircle.setLayoutParams(layoutParams);
                    //holder.container.addView(areaCircle);
                    if(index % 5 == 0){
                        line = new LinearLayout(mContext);
                        line.setLayoutParams(lineParams);
                        holder.container.addView(line);
                    }
                    line.addView(areaCircle);
                    index++;
                }

            }

            if (mIsPrizeTime && position == 0) {
                holder.container.setVisibility(View.GONE);
                holder.prize_time.setVisibility(View.VISIBLE);
            } else {
                holder.container.setVisibility(View.VISIBLE);
                holder.prize_time.setVisibility(View.GONE);
            }
        }
        return view;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public int getCount() {
        return getPrizeHistroyResponses.size();
    }


    static class ViewHolder {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.time_txt)
        TextView time_txt;
        @BindView(R.id.container)
        FlowLayout container;
        @BindView(R.id.prize_time)
        TextView prize_time;
        @BindView(R.id.item_container)
        LinearLayout itemContainerBg;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder1 {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.time_txt)
        TextView time_txt;
        @BindView(R.id.container)
        LinearLayout container;
        @BindView(R.id.prize_num)
        TextView prizeNum;
        @BindView(R.id.descp)
        TextView descp;
        @BindView(R.id.prize_time)
        TextView prize_time;
        @BindView(R.id.item_container)
        LinearLayout itemContainer;

        ViewHolder1(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder2 {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.container)
        LinearLayout container;
        @BindView(R.id.timeline)
        ImageView timeline;
        @BindView(R.id.prize_time)
        TextView prize_time;
@BindView(R.id.ll_content)
        LinearLayout llContent;
        ViewHolder2(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
